package com.learn.transform;

import org.apache.flink.api.common.RuntimeExecutionMode;
import org.apache.flink.api.common.typeinfo.TypeInformation;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.ProcessFunction;
import org.apache.flink.util.Collector;
import org.apache.flink.util.OutputTag;

/**
 * @create: 2023-04-18 12:34
 * @author: Mr.Du
 * --------------
 * @notes:
 * split将一个DataStream拆分成两个或多个DataStream，
 * select获取分流后对应的数据
 * split方法已经过期
 **/
public class StreamSideOutputDemo {

    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

        env.setRuntimeMode(RuntimeExecutionMode.AUTOMATIC);

        DataStreamSource<Integer> ds = env.fromElements(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
        OutputTag<Integer> oddTag = new OutputTag<>("奇数", TypeInformation.of(Integer.class));
        OutputTag<Integer> evenTag = new OutputTag<>("偶数", TypeInformation.of(Integer.class));

        SingleOutputStreamOperator<Integer> result = ds.process(new ProcessFunction<Integer, Integer>() {
            @Override
            public void processElement(Integer in, ProcessFunction<Integer, Integer>.Context ctx, Collector<Integer> out) throws Exception {
                if (in % 2 == 0) {
                    ctx.output(evenTag, in);
                } else {
                    ctx.output(oddTag, in);
                }
            }
        });


        DataStream<Integer> oddSideOutput = result.getSideOutput(oddTag);
        DataStream<Integer> evenSideOutput = result.getSideOutput(evenTag);

        oddSideOutput.print("奇数：");
        evenSideOutput.print("偶数：");

        env.execute("");
    }

}
